package com.jedisdemo;

import com.alibaba.fastjson.JSON;
import com.spring.bean.MyBean;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: dong peng
 * @date: 2019/7/9 17:37
 * @description:
 */

public class JedisClusterTest {

    private static JedisCluster jedisCluster = init();
    static {
        init();
    }
    public static void main(String[] args) {
        JedisClusterTest jedisClusterTest = new JedisClusterTest();

        jedisClusterTest.testSetGet();
        jedisClusterTest.testList();
        jedisClusterTest.testListLen();
        jedisClusterTest.testListLpop();
    }

    public void testSetGet() {
        String setStr = jedisCluster.set("test","my redis cluster test");
        System.out.println("testSetGet()set: "+setStr);
        String str = jedisCluster.get("test");
        System.out.println("testSetGet()get: "+str);

    }

    public void testList() {
        Long lpushR = jedisCluster.lpush("list:push","1","3","2");
        System.out.println("testList()push: "+lpushR);
    }

    public void testListLpop() {
        String str = jedisCluster.lpop("list:push");
        System.out.println("testList()pop: "+str);
    }

    public void testListLen() {
        Long llen = jedisCluster.llen("list:push");
        System.out.println("testListLen()llen: "+llen);
    }


    public static JedisCluster init(){
        //创建jedisCluster对象，有一个参数 nodes是Set类型，Set包含若干个HostAndPort对象
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("19.19.22.215",7000));
        nodes.add(new HostAndPort("19.19.22.215",7001));
        nodes.add(new HostAndPort("19.19.22.215",7002));
        nodes.add(new HostAndPort("19.19.22.215",7003));
        nodes.add(new HostAndPort("19.19.22.215",7004));
        nodes.add(new HostAndPort("19.19.22.215",7005));
        JedisCluster jedisCluster = new JedisCluster(nodes);
        return jedisCluster;
    }
}
